<template>
    <div class="simulation-run-container">
        <MarsMap :url="configUrl" :options="configOptions" @onload="onMapload"></MarsMap>
        <div class="s-r-top">
            <div class="s-r-top-left">
                <div class="s-r-top-left__item">
                    <el-tag>{{ isRunning ? '⏵ 仿真运行中' : '⏸ 仿真已暂停' }}</el-tag>
                </div>
                <div class="s-r-top-left__item">
                    <el-tag>仿真时刻: 0秒</el-tag>
                </div>
                <div class="s-r-top-left__item">
                    <el-tag>倍速: 10X</el-tag>
                </div>
                <div class="s-r-top-left__item">
                    <el-tag>CPU用量: 4m</el-tag>
                </div>
                <div class="s-r-top-left__item">
                    <el-tag>内存用量: 1.48Gi</el-tag>
                </div>
                <div class="s-r-top-left__item">
                    <el-tag>出站流量: 0.96Kbps</el-tag>
                </div>
                <div class="s-r-top-left__item">
                    <el-tag>入站流量: 5.71Kbps</el-tag>
                </div>
            </div>
            <div class="s-r-top-middle">白方视角</div>
            <div class="s-r-top-right">
                <div class="s-r-top-right__item" @click="$router.go(-1)">
                    <el-tag>返回</el-tag>
                </div>
                <div class="s-r-top-right__item">
                    <el-tag>减速</el-tag>
                </div>
                <div class="s-r-top-right__item">
                    <el-tag>暂停</el-tag>
                </div>
                <div class="s-r-top-right__item">
                    <el-tag>加速</el-tag>
                </div>
            </div>
        </div>
        <div class="s-r-middle">
            <div class="s-r-left">
                <div class="title">红方实体</div>
                <Tree class="content" ref="redEntityTreeRef" :isGeneralFolderAndFile="true" :treeData="entityList"
                    :topQuery="false" :showMenu="false" :expandOnClickNode="false" @nodeClick="nodeClick"></Tree>
            </div>
            <div class="s-r-right">
                <div class="title">蓝方实体</div>
                <Tree class="content" ref="blueEntityTreeRef" :isGeneralFolderAndFile="true" :treeData="entityList"
                    :topQuery="false" :showMenu="false" :expandOnClickNode="false" @nodeClick="nodeClick"></Tree>
            </div>
        </div>

        <div class="s-r-bottom">

            <div class="message">
                <div class="title">红方消息</div>
                <MessageScroller class="message-scroller" :messages="messages"></MessageScroller>
            </div>
            <div class="entity-info">
                <div class="title">实体信息</div>
                <div class="info">
                    <div class="info-item">名称：<el-tag>航空母舰</el-tag></div>
                    <div class="info-item">型号：<el-tag>11000001</el-tag></div>
                    <div class="info-item">速度：<el-tag>100</el-tag></div>
                    <div class="info-item">经度：<el-tag>100</el-tag></div>
                    <div class="info-item">维度：<el-tag>10</el-tag></div>
                    <div class="info-item">高度：<el-tag>100</el-tag></div>
                </div>
                <div class="function-button"></div>
            </div>
            <div class="message">
                <div class="title">蓝方消息</div>
                <MessageScroller class="message-scroller" :messages="messages"></MessageScroller>
            </div>
        </div>
    </div>
</template>

<script>
import MessageScroller from "./components/simulation-room/message-scroller.vue";
import MarsMap from "@/views/common-components/mars-map.vue";
import Tree from "@/components/LeftTree/tree.vue";
import { getEntityTree } from "@/api/myspace/simulation-run-tool/simulation-run";

export default {
    data() {
        const basePathUrl = window.basePathUrl || ''
        return {
            configUrl: basePathUrl + '/config/map.json',
            configOptions: {
                "control": {
                    "homeButton": false,
                    "sceneModePicker": false,
                    "zoom": {
                        "enabled": false,
                    },
                },
            },
            isRunning: false,
            messages: [],
            nextId: 1,
            entityList: [],
        }
    },

    computed: {
    },
    components: {
        Tree,
        MarsMap,
        MessageScroller
    },
    created() {
        this.getEntityTree();
    },
    mounted() {

        // 模拟添加消息
        setInterval(() => {
            this.addMessage(`仿真事件 ${this.nextId}: 系统运行正常。`)
        }, 1000)

        setInterval(() => {
            this.addMessage(`仿真事件 ${this.nextId}: 系统运行正常系统运行正常系统运行正。`)
        }, 1000)

        setInterval(() => {
            this.addMessage(`仿真事件 ${this.nextId}: 系统运行正常系统运行正常系统运行正常系统运行正常系统运行正常系统运行正常系统。`)
        }, 1000)

        // setInterval(() => {
        //     this.addMessage(`仿真事件 ${this.nextId}: 系统运行正常。`)
        // }, 1000)


    },
    beforeDestroy() {
    },

    methods: {
        getEntityTree() {
            getEntityTree().then((response) => {
                if (response.code === 200) {
                    this.entityList = response.data[0].commonTreeList;
                }
            });
        },
        onMapload(map) {
            console.log(map);
        },
        nodeClick() {
            console.log("nodeClick");
        },
        addMessage(content) {
            if (this.messages.length > 1000) {
                this.messages.splice(0, this.messages.length - 1000); // 删除前面的元素，只保留最后1000条
            }
            let size = parseInt(content.length / 17) + 1 == 1? 30: parseInt(content.length / 17) + 1 == 2? 48: 65
            // console.log(size);
            // 30:48:65
            this.messages.push({
                size: size,
                id: this.nextId,
                content: size + content,
                timestamp: new Date(),
            })
            this.nextId++;
        },
    },
}
</script>

<style lang="scss" scoped>
.simulation-run-container {
    width: 100%;
    height: 100%;
    user-select: none;

    .title {
        display: flex;
        justify-content: center;
        align-items: center;
        height: 35px;
        color: var(--ui-text-color);
        font-size: var(--ui-font-size-base);
    }

    .s-r-top {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 35px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        background-color: rgba(0, 0, 0, 0.3);

        .s-r-top-left {
            display: flex;
            flex: 1;
            justify-content: left;

            .s-r-top-left__item {
                cursor: default;
                margin-left: 10px;
            }
        }

        .s-r-top-middle {
            display: flex;
            flex: 1;
            align-items: center;
            justify-content: center;
            color: var(--ui-text-color);
            font-size: var(--ui-font-size-title);
            font-weight: var(--ui-font-weight-bold);
            border-radius: 5px;
        }

        .s-r-top-right {
            display: flex;
            flex: 1;
            justify-content: flex-end;

            .s-r-top-right__item {
                cursor: pointer;
                margin-right: 5px;
            }
        }
    }

    .s-r-left {
        position: absolute;
        top: 40px;
        left: 0;
        height: calc(100% - 305px);
        width: 300px;
        background-color: rgba(0, 0, 0, 0.3);

        .content {
            padding-left: 10px;
        }
    }

    .s-r-right {
        position: absolute;
        top: 40px;
        right: 0;
        height: calc(100% - 305px);
        width: 300px;
        background-color: rgba(0, 0, 0, 0.3);

        .content {
            padding-right: 10px;
        }
    }

    ::v-deep .el-tree-node__content {
        border-bottom: 1px solid rgba(0, 0, 0, 0) !important;
    }

    .s-r-bottom {
        position: absolute;
        left: 0;
        bottom: 26px;
        width: 100%;
        height: 235px;
        background-color: rgba(0, 0, 0, 0.3);
        display: flex;
        justify-content: space-between;
        align-items: center;

        .entity-info {
            height: 100%;
            width: calc(100% - 650px);
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            align-items: center;

            .info {
                display: flex;
                justify-content: space-around;
                align-items: center;
                width: 100%;
                color: var(--ui-text-color);
                border-radius: 8px;
                height: 70px;
                background-color: rgba(0, 0, 0, 0.4);

                .el-col {
                    display: flex;
                    justify-content: center;
                    align-items: center;
                }
            }

            .function-button {
                width: 100%;
                height: 90px;
                border-radius: 8px;
                background-color: rgba(0, 0, 0, 0.4);
                margin-bottom: 10px;

            }
        }

        .message {
            width: 300px;

            .message-scroller {
                height: 200px;
                width: 300px;
                font-size: var(--ui-font-size-base);
            }
        }
    }
}
</style>